<?php
declare(strict_types=1);

namespace Modules\Admin\Service;

use HiCommon\Helper;
use HiCommon\Repository\BannerRepository;
use HiCommon\Repository\UserInviteSetRepository;

class BannerService
{


    /**
     *    获取banner列表
     *    hinq 2019 08 27
     *
     **/
    public function get_list(array $arr_search)
    {
        $arr_list = BannerRepository::get_list((array)$arr_search);
        $arr_list->appends($arr_search);//添加链接参数
        $arr_list = list_page_data($arr_list, $arr_search);
        $types = ['1' => '小程序-首页-banner图', '3' => '用户端-道路救援banner图', '4' => '用户端-首页-活动主页banner', '5' => '商户端-APP引导页', '6' => '用户端-年检维修主页'];
        foreach ($arr_list['data'] as $key => &$value) {
            $value['img'] = oss_full_url($value['img']);
            $value['type_msg'] = $types[$value['type']];
        }
        return hello_success('成功', $arr_list);
    }


    /**
     *    根据指定的条件查询
     *    hinq 2019 08 27
     *
     **/
    public function get_where_list(array $arrWhere)
    {
        return BannerRepository::get_where_list($arrWhere);
    }


    /**
     *    添加banner
     *    hinq 2019 08 27
     *
     **/
    public function add_one(array $arrInput)
    {
        try {
            //添加
            $arrInput['status'] = 1;
            $arrInput['link_url'] = empty($arrInput['link_url']) ? '#' : $arrInput['link_url'];
            $res = BannerRepository::add_one((array)$arrInput);
            if (empty($res)) {
                return hello_error('添加失败');
            }
            return hello_success('添加成功', $res);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }


    /**
     *    获取指定的banner
     *    hinq 2019 08 27
     *
     **/
    public function get_one(int $nId)
    {
        $arr_info = BannerRepository::get_one((int)$nId);
        $arr_info['img_msg'] = oss_full_url($arr_info['img']);   //返回图片的绝对路径
        return $arr_info;
    }


    /**
     *    编辑banner数据
     *    hinq 2019 08 27
     *
     **/
    public function edit_one(array $arrInput)
    {
        try {
            //编辑
            unset($arrInput['_token']);
            unset($arrInput['file']);
            $arrInput['link_url'] = empty($arrInput['link_url']) ? '#' : $arrInput['link_url'];
            $res = BannerRepository::edit_one((array)$arrInput);
            if (empty($res)) {
                return hello_error('编辑失败');
            }
            return hello_success('编辑成功', $res);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }


    /**
     *    banner设置
     *    hinq 2019 10 08
     *
     **/
    public function get_set()
    {


        $arr_list = UserInviteSetRepository::get_one_to_where((array)['type' => 2], (array)['field' => ['id', 'p_num', 'num']]);
        return $arr_list;
    }

    /**
     *  保存banner设置
     *    hinq 2019 10 08
     *
     **/
    public function set_update(array $arr_input)
    {
        try {
            unset($arr_input['_token']);
            if (empty($arr_input['p_num'])) {
                return hello_error('请设置张数');
            }
            if (empty($arr_input['num'])) {
                return hello_error('请设置时间间隔');
            }
            $arr_input['id'] = 3;
            $res = UserInviteSetRepository::edit_one((array)$arr_input);
            if (empty($res)) {
                return hello_error('设置失败');
            }
            return hello_success('设置成功', $res);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }
}